﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;

namespace Tayninh2011
{
    sealed class TnExcuteCalculator:ITnExcuteCalculator
    {
        #region Fields

        private delegate void TinhgiaDatDelegate();
        private static TinhgiaDatDelegate excute;

        private event TinhgiaDatDelegate tinhgiaEvent;

        private delegate void steps(object o,RunWorkerCompletedEventArgs e);

        private string log = String.Empty;
        private TnThua thua = null;
        private TnDuong duong = null;
        private TnHem hem = null;
        private TnHem hem1 = null;
        private TnDuong duong50 = null;
        private TnDuong duong05 = null;
        private TnDuong duong1 = null;
        //private TnCalculatorFactory calcFactory;
        //private TnToolsCalculator calcTool;
        private Boolean isFinished = false;
        private Boolean isCalculating = false;
        private TnThuaFactory thuaFactory = new TnThuaFactory();
        private TnDuongFactory duongFactory = new TnDuongFactory();
        private TnHemFactory hemFactory = new TnHemFactory();
        private ITnToolsWorkspace toolWsp = new TnTools();
        private ITnToolsTable toolTbl = new TnTools();
        private static Dictionary<int, TinhgiaDatDelegate> dicTask = new Dictionary<int, TinhgiaDatDelegate>();
        private static List<int> lstTask = new List<int>();
        private int processPercentage = 0;
        private int bip = 0;
        //private TnToolsCalculator calcTool3010 = null;
        //private TnToolsCalculator calcTool3020 = null;
        //private TnToolsCalculator calcTool31xx32xx = null;

        
        private FrmCash frmCash=null;

        #endregion Fields

        #region Properties


        #endregion Properties

        #region singleton

        private static readonly TnExcuteCalculator meClass = new TnExcuteCalculator();

        public static TnExcuteCalculator CallMe
        {
            get { return meClass; }
        }

        #endregion singleton

        

        private TnExcuteCalculator()
        {
            excute += ()=>initTask(null);
            frmCash = FrmCash.CallMe;
            lstTask.Add(TnExcuteCalcType.Init);
            //this.calcFactory = TnCalculatorFactory.CallMe;
            dicTask.Add(TnExcuteCalcType.THUA_MAT_TIEN,()=> tinhThuaMattien(new steps(continueTinhThuaSau50m)));
            dicTask.Add(TnExcuteCalcType.THUA_MAT_TIEN_VA_HEM, ()=>tinhThuaMattien(new steps(continueTinhThuaSau50m2)));
            dicTask.Add(TnExcuteCalcType.THUA_HEM, ()=>tinhThuaHem(null));
            //calcTool3010 = RootForm.CalcFactory.GetCalculator(EnumModeCalc.mode3010);
            //calcTool3020 = RootForm.CalcFactory.GetCalculator(EnumModeCalc.mode3020);
            //calcTool31xx32xx = RootForm.CalcFactory.GetCalculator(EnumModeCalc.mode31xxAnd32xx);
        }
        private void initTask(steps next) { }


        #region Chi tinh thua mat tien

        private void tinhThuaMattien(steps next)
        {
            //calcFactory = new TnCalculatorFactory(RootForm.SdeWorkspace, RootForm.SdeEnvironment, RootForm.UserInfo);
            //calcFactory.SetParams(RootForm.SdeWorkspace, RootForm.SdeEnvironment, RootForm.UserInfo);
            this.log = "Đang tính cho thửa mặt tiền";
            //ITnToolsWorkspace toolWsp = new TnTools();
            //ITnToolsTable toolTbl = new TnTools();
            toolTbl.TnFormatTable(RootForm.SdeWorkspace, RootForm.NameThua);
            
            //MessageBox.Show("ok");
            //TnThuaFactory thuaFactory = new TnThuaFactory();
            //TnDuongFactory duongFactory = new TnDuongFactory();
            toolTbl.TnFormatColumn(RootForm.SdeWorkspace, RootForm.NameThua, TnToolsCalculator.fcachtinh, "");
            thua = thuaFactory.GetThua("3010", RootForm.SdeWorkspace);
            //duong = duongFactory.GetDuong(EnumLoaiDuong.dp_loai1, this._sdeWorkspace);
            duong50 = duongFactory.GetDuong(EnumLoaiDuong.dp_loai1_buff_50, RootForm.SdeWorkspace);

            duong05 = duongFactory.GetDuong(EnumLoaiDuong.dp_loai1_buff_05, RootForm.SdeWorkspace);

            
            bip = 1;

            RootForm.CalcTool3010.AddExTool(EnumExTools.spatialRelTool);
            RootForm.CalcTool3010.ClearInputValues();
            RootForm.CalcTool3010.SetInputValues(ref thua, ref duong05, EnumTask.addInfo3010);
            RootForm.CalcTool3010.SetInputValues(ref thua, ref duong50, EnumTask.landPrice3010);
            RootForm.CalcTool3010.AddTask(EnumTask.addInfo3010);
            RootForm.CalcTool3010.AddTask(EnumTask.landPrice3010);
            RootForm.CalcTool3010.BackgroundWorker.ProgressChanged += new ProgressChangedEventHandler(BackgroundWorker_ProgressChanged);
            if (next != null)
            {
                RootForm.CalcTool3010.BackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(next);
            }
            
            //MessageBox.Show(thua.FeatureClass.AliasName + "--"+duong05.FeatureClass.AliasName);

            RootForm.CalcTool3010.Start();

        }

        void continueTinhThuaSau50m(object sender, RunWorkerCompletedEventArgs e)
        {
            tinhThuaSau50m(null);
        }

        private void tinhThuaSau50m(steps next)
        {
            //calcFactory = new TnCalculatorFactory(RootForm.SdeWorkspace, RootForm.SdeEnvironment, RootForm.UserInfo);
            this.log = "Đang tính cho các thửa có đất sau mặt tiền 50m";
            
            bip = 1;
            //MessageBox.Show(calcTool.Name);
            RootForm.CalcTool3020.AddExTool(EnumExTools.dataManager, EnumExTools.spatialRelTool, EnumExTools.spatialTool);
            RootForm.CalcTool3020.BackgroundWorker.ProgressChanged += new ProgressChangedEventHandler(BackgroundWorker_ProgressChanged);
            if (next == null)
            {
                RootForm.CalcTool3020.BackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorker_RunWorkerCompleted);
            }
            else
            {
                RootForm.CalcTool3020.BackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(next);
            }
            //thua.FeatureCursor = thua.FeatureClass.Search(null, false);
            duong50.FeatureCursor = duong50.FeatureClass.Search(null, false);
            duong50.Feature = duong50.FeatureCursor.NextFeature();

            RootForm.CalcTool3020.ClearInputValues();
            RootForm.CalcTool3020.SetInputValues(ref thua, ref duong50, EnumTask.landPrice3020);

            RootForm.CalcTool3020.AddTask(EnumTask.landPrice3020);

            string[] a = RootForm.CalcTool3020.ListTasks();
            //MessageBox.Show(a.Length.ToString()+","+a[0]+","+a[1]);
            RootForm.CalcTool3020.Start();
        }

        #endregion Chi tinh thua mat tien

        #region tinh thua mat tien va thua hem

        //private void tinhThuaMattienThenThuaHem(steps next)
        //{
        //    calcFactory = new TnCalculatorFactory(RootForm.SdeWorkspace, RootForm.SdeEnvironment, RootForm.UserInfo);
        //    this.log = "Đang tính cho thửa mặt tiền";
        //    ITnToolsWorkspace toolWsp = new TnTools();
        //    ITnToolsTable toolTbl = new TnTools();
        //    toolTbl.TnFormatTable(RootForm.SdeWorkspace, RootForm.NameThua);
        //    //MessageBox.Show("ok");
        //    TnThuaFactory thuaFactory = new TnThuaFactory();
        //    TnDuongFactory duongFactory = new TnDuongFactory();
        //    thua = thuaFactory.GetThua("3010", RootForm.SdeWorkspace);
        //    //duong = duongFactory.GetDuong(EnumLoaiDuong.dp_loai1, this._sdeWorkspace);
        //    duong50 = duongFactory.GetDuong(EnumLoaiDuong.dp_loai1_buff_50, RootForm.SdeWorkspace);

        //    duong05 = duongFactory.GetDuong(EnumLoaiDuong.dp_loai1_buff_05, RootForm.SdeWorkspace);

        //    calcTool = calcFactory.GetCalculator(EnumModeCalc.mode3010);
        //    calcTool.AddExTool(EnumExTools.spatialRelTool);
        //    calcTool.ClearInputValues();
        //    calcTool.SetInputValues(ref thua, ref duong05, EnumTask.addInfo3010);
        //    calcTool.SetInputValues(ref thua, ref duong50, EnumTask.landPrice3010);
        //    calcTool.AddTask(EnumTask.addInfo3010);
        //    calcTool.AddTask(EnumTask.landPrice3010);
        //    calcTool.BackgroundWorker.ProgressChanged += new ProgressChangedEventHandler(BackgroundWorker_ProgressChanged);
        //    calcTool.BackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(next);
        //    calcTool.Start();
        //}

        void continueTinhThuaSau50m2(object sender, RunWorkerCompletedEventArgs e)
        {
            //tinhThuaSau50mThenThuaHem();
            tinhThuaSau50m(continueTinhThuaHem);
        }

        //private void tinhThuaSau50mThenThuaHem()
        //{
        //    //calcFactory = new TnCalculatorFactory(RootForm._sdeWorkspace, RootForm._sdeEnvironment, RootForm._arrUserInfo);
        //    this.log = "Đang tính cho các thửa có đất sau mặt tiền 50m";
        //    calcTool = calcFactory.GetCalculator(EnumModeCalc.mode3020);
        //    //MessageBox.Show(calcTool.Name);
        //    calcTool.AddExTool(EnumExTools.dataManager, EnumExTools.spatialRelTool, EnumExTools.spatialTool);
        //    calcTool.BackgroundWorker.ProgressChanged += new ProgressChangedEventHandler(BackgroundWorker_ProgressChanged);
        //    calcTool.BackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(continueTinhThuaHem);
        //    //thua.FeatureCursor = thua.FeatureClass.Search(null, false);
        //    duong50.FeatureCursor = duong50.FeatureClass.Search(null, false);
        //    duong50.Feature = duong50.FeatureCursor.NextFeature();

        //    calcTool.ClearInputValues();
        //    calcTool.SetInputValues(ref thua, ref duong50, EnumTask.landPrice3020);

        //    calcTool.AddTask(EnumTask.landPrice3020);

        //    string[] a = calcTool.ListTasks();
        //    //MessageBox.Show(a.Length.ToString()+","+a[0]+","+a[1]);
        //    calcTool.Start();
        //}

        void continueTinhThuaHem(object sender, RunWorkerCompletedEventArgs e)
        {
            tinhThuaHem(null);
        }
        private void tinhThuaHem(steps next)
        {
            //calcFactory = new TnCalculatorFactory(RootForm.SdeWorkspace, RootForm.SdeEnvironment, RootForm.UserInfo);
            this.log = "Đang tính cho thửa hẻm";
            //ITnToolsWorkspace toolWsp = new TnTools();
            //ITnToolsTable toolTbl = new TnTools();
            //TnThuaFactory thuaFactory = new TnThuaFactory();
            //TnDuongFactory duongFactory = new TnDuongFactory();
            //TnHemFactory hemFactory = new TnHemFactory();

            toolTbl.TnFormatColumn(RootForm.SdeWorkspace, RootForm.NameHem, TnToolsCalculator.fghichuhem, "");
            //toolTbl.TnFormatColumn(RootForm._sdeWorkspace, "tn_hem", "soduongchinh", 0);
            toolTbl.TnFormatColumn(RootForm.SdeWorkspace, RootForm.NameHem, TnToolsCalculator.fgiadathem, 0);
            toolTbl.TnFormatColumn(RootForm.SdeWorkspace, RootForm.NameHem, TnToolsCalculator.fsodgc, 0);
            toolTbl.TnFormatColumn(RootForm.SdeWorkspace, RootForm.NameHem, TnToolsCalculator.fhemchinh, -1);
            toolTbl.TnFormatColumn(RootForm.SdeWorkspace, RootForm.NameHem, TnToolsCalculator.fsohmc, 0);
            toolTbl.TnFormatColumn(RootForm.SdeWorkspace, RootForm.NameHem, TnToolsCalculator.ftenhmc, "");
            toolTbl.TnFormatColumn(RootForm.SdeWorkspace, RootForm.NameHem, TnToolsCalculator.ftendgc, "");
            toolTbl.TnFormatColumn(RootForm.SdeWorkspace, RootForm.NameThua, TnToolsCalculator.fsomathem, 0);
            toolTbl.TnFormatColumn(RootForm.SdeWorkspace, RootForm.NameThua, TnFieldName.THUA_GHI_CHU, "",TnFieldName.THUA_SO_MAT_TIEN+"='0'");

            thua = thuaFactory.GetThua("3010", RootForm.SdeWorkspace);//gia tri 3010 ko co y nghia
            duong1 = duongFactory.GetDuong(EnumLoaiDuong.dp_loai1_buff_1, RootForm.SdeWorkspace);
            hem = hemFactory.GetHem(EnumLoaiHem.hemDuongPho, RootForm.SdeWorkspace, RootForm.SdeEnvironment, RootForm.UserInfo);

            //hem1=hemFactory.GetHem(EnumLoaiHem.hemBuffer1m,this._sdeWorkspace,this._sdeEnvironment);

            
            RootForm.CalcTool31xx32xx.AddExTool(EnumExTools.dataManager, EnumExTools.spatialRelTool, EnumExTools.spatialTool);
            RootForm.CalcTool31xx32xx.BackgroundWorker.ProgressChanged += new ProgressChangedEventHandler(BackgroundWorker_ProgressChanged);
            RootForm.CalcTool31xx32xx.BackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorker_RunWorkerCompleted);
            //duong1.FeatureCursor = duong1.FeatureClass.Search(null, false);

            RootForm.CalcTool31xx32xx.ClearInputValues();
            RootForm.CalcTool31xx32xx.SetInputValues(ref thua, ref duong1, ref hem, EnumTask.addInfoHemChinh);
            RootForm.CalcTool31xx32xx.SetInputValues(ref thua, ref duong1, ref hem, EnumTask.addInfoHemPhu);
            RootForm.CalcTool31xx32xx.SetInputValues(ref thua, EnumTask.addInfo31xx);
            RootForm.CalcTool31xx32xx.SetInputValues(ref thua, EnumTask.addInfo32xx);

            RootForm.CalcTool31xx32xx.AddTask(EnumTask.addInfoHemChinh);
            RootForm.CalcTool31xx32xx.AddTask(EnumTask.addInfoHemPhu);
            RootForm.CalcTool31xx32xx.AddTask(EnumTask.addInfo31xx);
            RootForm.CalcTool31xx32xx.AddTask(EnumTask.addInfo32xx);

            RootForm.CalcTool31xx32xx.Start();
        }

        #endregion tinh thua mat tien va thua hem

        #region BackgroundWorker Handler
        void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            isFinished = true;
            isCalculating = false;
            //frmCash.actionComplete();
            
        }
       
        
        
        void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            isCalculating = true;
            isFinished = false;
            processPercentage = e.ProgressPercentage;

            //MessageBox.Show(e.ProgressPercentage.ToString());
            //this.label1.Text = e.ProgressPercentage.ToString() + "%";
        }
        #endregion BackgroundWorker Handler



        #region ITnExcuteCalculator Members

        void ITnExcuteCalculator.Start()
        {
            excute();
        }

        void ITnExcuteCalculator.AddTask(int excuteCalcType)
        {
            foreach (int i in lstTask)
            {
                if (excuteCalcType == i)
                {
                    return;
                }
            }
            lstTask.Add(excuteCalcType);
            excute += dicTask[excuteCalcType];
        }

        void ITnExcuteCalculator.RemoveTask(int excuteCalcType)
        {
            int len = lstTask.Count;
            for (int i = 0; i < len; i++)
            {
                if (excuteCalcType == lstTask[i])
                {
                    lstTask.RemoveAt(i);
                    excute -= dicTask[lstTask[i]];
                    return;
                }
            }
        }


        bool ITnExcuteCalculator.IsCalculating
        {
            get
            {
                return this.isCalculating;
            }
        }

        bool ITnExcuteCalculator.IsFinished
        {
            get
            {
                return this.isFinished;
            }
        }

        void ITnExcuteCalculator.RemoveAll()
        {
            lstTask = new List<int>();
            lstTask.Add(TnExcuteCalcType.Init);

            Delegate[] dele;
            dele = excute.GetInvocationList();
            foreach (TinhgiaDatDelegate task in dele)
            {
                excute -= task;
            }
            excute += ()=>initTask(null);
            
        }

        string ITnExcuteCalculator.Log
        {
            get { return this.log; }
        }

        int ITnExcuteCalculator.ProgressPercentage
        {
            get { return this.processPercentage; }
        }

        //TnToolsCalculator ITnExcuteCalculator.CalcTool
        //{
        //    get { return this.calcTool; }
        //}

        int ITnExcuteCalculator.Bip
        {
            get { return bip; }
            set { bip = value; }
        }

        TnToolsCalculator ITnExcuteCalculator.CalcTool3010
        {
            get { return RootForm.CalcTool3010; }
        }

        TnToolsCalculator ITnExcuteCalculator.CalcTool3020
        {
            get { return RootForm.CalcTool3020; }
        }

        TnToolsCalculator ITnExcuteCalculator.CalcTool31xx32xx
        {
            get { return RootForm.CalcTool31xx32xx; }
        }

        #endregion

        #region ITnExcuteCalculator Members


        TnToolsCalculator ITnExcuteCalculator.CalcTool
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
}
